package com.cocotea.valid.service.impl;

import com.cocotea.valid.annotations.*;
import com.cocotea.valid.constant.AnnotationNameConstant;
import com.cocotea.valid.constant.MessageConstant;
import com.cocotea.valid.service.IValidDispatcher;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class ValidDispatcherImpl implements IValidDispatcher {
    @Override
    public String doValid(Field field, Object value, String annotationName) {
        switch (annotationName) {
            case AnnotationNameConstant.NOT_NULL: return handleNotNull(field, value);
            case AnnotationNameConstant.NOT_BLANK: return handleNotBlank(field, value);
            case AnnotationNameConstant.REGEX_VALID: return handleRegexValid(field, value);
            case AnnotationNameConstant.STR_RANGE: return handleStrRange(field, value);
            case AnnotationNameConstant.NOT_EMPTY: return handleNotEmpty(field, value);
            default: break;
        }
        return MessageConstant.SUCCESS;
    }

    private String handleNotEmpty(Field field, Object value) {
        NotEmpty notEmpty = field.getAnnotation(NotEmpty.class);
        if (value instanceof List) {
            List<?> list = (List<?>) value;
            if (list.isEmpty()) {
                return notEmpty.value();
            }
        } else if (value instanceof String) {
            if (((String) value).length() == 0) {
                return notEmpty.value();
            }
        } else if (value instanceof Map) {
            Map<?,?> map = (Map<?, ?>) value;
            if (map.isEmpty()) {
                return notEmpty.value();
            }
        }
        return MessageConstant.SUCCESS;
    }

    private String handleStrRange(Field field, Object value) {
        StrRange strRange = field.getAnnotation(StrRange.class);
        String valueStr = value.toString();
        if (valueStr.length() < strRange.min() || valueStr.length() > strRange.max()) {
            return strRange.message();
        }
        return MessageConstant.SUCCESS;
    }

    public String handleRegexValid(Field field, Object value) {
        RegexValid regexValid = field.getAnnotation(RegexValid.class);
        boolean require = regexValid.require();
        // 校验必须项
        if (require) {
            if (value == null || "".equals(value)) {
                return regexValid.value();
            }
        }
        // 校验正则表达式是否为空
        String regex = regexValid.regex();
        if (regex == null || "".equals(regex)) {
            if (require) {
                return "正则表达式为空";
            }
        } else {
            // 校验正则表达式
            Pattern pattern = Pattern.compile(regex);
            if (value != null) {
                if (!pattern.matcher(value.toString()).matches()) {
                    return regexValid.value();
                }
            }
        }
        return MessageConstant.SUCCESS;
    }

    public String handleNotNull(Field field, Object value) {
        NotNull notNull = field.getAnnotation(NotNull.class);
        if (value == null) {
            return notNull.value();
        }
        return MessageConstant.SUCCESS;
    }

    public String handleNotBlank(Field field, Object value) {
        NotBlank notBlank = field.getAnnotation(NotBlank.class);
        if (value == null || "".equals(value)) {
            return notBlank.value();
        }
        return MessageConstant.SUCCESS;
    }
}
